/*
 * @Author: lin.zehong
 * @Date: 2019-07-22 10:02:25
 * @Last Modified by: lin.zehong
 * @Last Modified time: 2019-07-22 10:03:06
 * @Desc: 查看标签 与 新增标签；
 */
import React, { Component } from "react";
import { connect } from "dva";
import SelectModal from "./SelectModal";
import LookUserModal from "./LookUserModal";
import styles from "./SelectModalLabel.less";

/**
 * 部门人员选择 modal
 * @param dataArr 数组
 */

const mapDispatchToProps = dispatch => ({
  getOrgList() {
    return dispatch({
      type: "adddataSource/getOrgList",
      payload: {}
    });
  }
});

@connect(null, mapDispatchToProps)
class SelectModalLabel extends Component {

  state= {
    manager: [],
    managerName: [],
  }

  componentWillMount() {
    const { getOrgList } = this.props;
    getOrgList();
  }

  componentDidMount() {
    const { dataArr = [] } = this.props;
    if (dataArr) {
      const manager = dataArr.map(item => item.key);
      const managerName = dataArr.map(item => item.name);
      this.setState({ 
        managerName, 
        manager,
      });
    }
  }

  confirm = (e, desc) => {
    this.setState({ 
      managerName: desc, 
      manager: e,
    });
    const data = this.handleManager(desc, e);
    const { onSure } = this.props;
    onSure(data);
  };

  closeModal = () => {
    this.props.onCancel();
  };

  // 处理管理员数据
  handleManager = (descs, ids) => {
    const params = [];
    descs.forEach((item, index) => {
      params.push({name: item, key: ids[index] });
    });
    return params;
  }

  render() {
    const { manager, managerName } = this.state;
    const { title, disableChecked, dataArr = [] } = this.props;
    return (
      <div className={styles.root}>
        {
          disableChecked ? 
            <LookUserModal
              select={manager}
              selectdesc={managerName}
              closeModal={this.closeModal}
              confirm={this.confirm}
              visible
              isMutiple
              manager
              title={title}
              disableChecked={disableChecked}
              userLabel={dataArr}
          />
        :
          <SelectModal
            select={manager}
            selectdesc={managerName}
            closeModal={this.closeModal}
            confirm={this.confirm}
            visible
            isMutiple
            manager
            title={title}
            disableChecked={disableChecked}
          />
      }
      </div>
    );
  }
}

export default SelectModalLabel;
